package controller.trafficlights;

import controller.VehiclePriority;
import simulator.enums.Direction;
import simulator.enums.LaneType;

public class BusLight extends TrafficLight
{
	private final int MAX_GREEN_TIME = 3000;
	private final int MAX_ORANGE_TIME = 2000;
	private final int MAX_WAITING_TIME = 30000;
	
	public BusLight(Direction direction, LaneType type)
	{
		_direction = direction;
		_type = type;
		_state = TrafficLightState.red;
		_isActive = false;
	}
	
	@Override
	public void updatePriority(long elapsedTime) 
	{
		if(isOccupied())
		{
			_waitingTime += elapsedTime;
			if(_waitingTime >= MAX_WAITING_TIME)
			{
				setPriority(_priority++);
				_waitingTime = 0;
			}
		}
	}

	@Override
	public void update(long elapsedTime) 
	{
		_elapsedTime += elapsedTime;
		
		if(_state == TrafficLightState.red)
		{
			if(isOccupied())
			{
				changeState(TrafficLightState.green);
				_elapsedTime = 0;
			}
		}
		else if(_state == TrafficLightState.yellow)
		{
			if(_elapsedTime >= MAX_ORANGE_TIME)
			{
				changeState(TrafficLightState.red);
				_elapsedTime = 0;
			}
		}
		else if(_state == TrafficLightState.green)
		{
			if(_elapsedTime >= MAX_GREEN_TIME)
			{
				changeState(TrafficLightState.yellow);
				_elapsedTime = 0;
			}
		}
	}

	@Override
	public boolean isCompatibleWith(TrafficLight trafficLight) 
	{
		//For now bus lanes can only be active one at the time.
		return false;
	}
}
